#include "Compile_and_run.hpp"
#include "../Comm/httplib.h"

using namespace compile_and_run_ns;
using namespace httplib;

void Usage(char *proc)
{
    std::cout << "Usage:" << proc << "port" << std::endl;
    exit(1);
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
    }
    Server svr;

    // svr.Get("/hello",[](const Request& req,Response& response){
    //     response.set_content("hello bro","text/plain");
    // });

    svr.Post("/Compile_Run", [](const Request &req, Response &response)
 {
       std::string json_in = req.body;
       std::string json_out;

       if(!json_in.empty())
       {
           CompileAndRun::Start(json_in,&json_out);
           response.set_content(json_out,"application/json; charset=UTF-8");
        } 
        else
       {
            response.set_content("请求错误","text/html; charset=UTF-8");
       }    
   });

    svr.listen("0.0.0.0",atoi(argv[1]));
    return 0;
}

// Json::Value in_value;
// in_value["code"] = R"(
// #include<iostream>

// int main()
// {
//     while(true);
//     std::cout << "hello world" << std::endl;
//     return 0;
// }
// )";
// in_value["input"] = "";
// in_value["mem_limit"] = 10240 * 5; // 50M
// in_value["cpu_limit"] = 1;

// Json::StyledWriter writer;
// std::string json_in;

// json_in = writer.write(in_value);

// std::string json_out;
// CompileAndRun::Start(json_in, &json_out);

// std::cout << json_out << std::endl;
// Json::Value out_value;
// Json::Reader reader;

// reader.parse(json_out, out_value);
// std::string reason = out_value["reason"].asCString();
// std::cout << reason << std::endl;
